<?php


namespace Admin\Controller;

require 'vendor/autoload.php';

use JPush\Client as JPushClient;
/**
 * 公告控制器
 * Class AnnoucementController
 * @package Admin\Controller
 */
class AnnoucementController extends AdminController {


    public function annoucementList(){
        if(IS_POST){

        }else{
            $lists = M('annoucement')->order("addtime desc")->select();
            $this->assign('lists',$lists);
            $this->display();
        }
    }


    /*
     * 添加
     */
    public function annoucementAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            if(trim($post['title']) == ""){$this->error("公告标题不能为空");}
            //处理参数
            $data['content'] = htmlspecialchars($post['editorValue']);;
            $data['title'] = trim(I('post.title'));
            $data['addtime'] = time();
            $data['uname'] = ADMINNAME;

            //验证通过了
            $res = M('annoucement')->add($data);
            if($res){
                //消息推送
                $url = 'http://www.xxhlwcy.com/index.php?s=/Home/Article/news_detail/aid/'.$res;
                $jpush = new JPushClient(C('JPUSH_APP_KEY') , C('JPUSH_MASTER_SECRET'));
                //type 1=>annoucement
                $jpush->push()
                    ->setPlatform('all')
                    ->addAndroidNotification($data['title'],'',1,array('type'=>'1','url'=>$url))
                    ->addIosNotification($data['title'],'default','+1',true,'ios category',array('type'=>'1','url'=>$url))
                    ->addAllAudience()
                    ->send();
                $this->success('添加成功并推送');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
    }
    /*
     * 修改
     */
    public function annoucementEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            //处理参数
            //处理参数
            $data['title'] = trim(I('post.title'));
            if($data['title'] == ''){
                $this->error('公告不能为空');
            }
            $data['content'] = htmlspecialchars($post['editorValue']);

            $res = M('annoucement')->where(array('id'=>I('post.id')))->save($data);
            
            if(is_numeric($res)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $id = I('get.id');
            if($id){
                $data = M('annoucement')->where(array('id'=>I('get.id')))->find();
                $this->assign('data',$data);
                $this->display();
            }
        }
    }

    /*
     * 操作
     */
    public function updateStatus($method=null){
        if ( empty($_REQUEST['id']) ) {
            $this->error('请选择要操作的数据!');
        }
        switch ( strtolower($method) ){
            case 'deleteannoucement':
                $this->remove('annoucement');
                break;
            default:
                $this->error($method.'参数非法');
        }
    }


}
